﻿using System;

class MaximalElement
{
    static void Main()
    {
        int[] array = { 2, 6, 6, 2, -4, 16, -5, 20, 18, 0, 9, 1 };

        Console.WriteLine("Unsorted:");
        PrintArray(array);

        SortIntArray(array);
        Console.WriteLine("Ascending sort: ");
        PrintArray(array);

        SortIntArray(array, false);
        Console.WriteLine("Descending sort: ");
        PrintArray(array);
    }

    static void PrintArray(int[] arr)
    {
        for (int i = 0; i < arr.Length -1; i++)
        {
            Console.Write(arr[i] + ", ");
        }

        Console.WriteLine(arr[arr.Length - 1]);
        Console.WriteLine();
    }

    static void SortIntArray(int[] arr, bool ascending = true)
    {
        for (int i = 0; i < arr.Length - 1; i++)
        {
            int maxValue = FindMaxElement(arr, i + 1);

            if (arr[i] < maxValue)
            {
                int temp = arr[i];
                int maxIndex = Array.IndexOf(arr, maxValue, i + 1);
                arr[i] = arr[maxIndex];
                arr[maxIndex] = temp;
            }
        }

        if (ascending == false)
        {
            Array.Reverse(arr);
        }
    }

    static int FindMaxElement(int[] arr, int startIndex = 0)
    {
        int maxElement = int.MinValue;

        for (int i = startIndex; i < arr.Length; i++)
        {
            if (arr[i] > maxElement)
            {
                maxElement = arr[i];
            }
        }

        return maxElement;
    }
}
